//
//  HelpCell.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/9.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "HelpCell.h"
#import "HelpButton.h"
@interface HelpCell ()
@property (nonatomic, weak) UILabel *answerLabel;         /**< 问题 */
@property (nonatomic, weak) HelpButton *showBtn;       /**< 收起按钮 */
@property (nonatomic, weak) UILabel *contentLabel;         /**< 内容 */
@property (nonatomic, weak)  UIView *lineView;
@end
@implementation HelpCell
+ (instancetype) cellWithTable:(UITableView *)tableview{
    static NSString *ID = @"HelpCell";
    HelpCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[HelpCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor=RGBColor(37, 41, 61);
        //问题
        UILabel *answerLabel = [[UILabel alloc] init];
        answerLabel.font = [UIFont boldSystemFontOfSize:16];
        answerLabel.textColor = RGBColor(204, 204, 204);
        self.answerLabel = answerLabel;
        [self addSubview:answerLabel];
        
        //显示收起
        HelpButton *showBtn = [HelpButton buttonWithType:UIButtonTypeCustom];
//        [showBtn setTitleColor:RGB_COLOR(12, 29, 56) forState:UIControlStateNormal];
        [showBtn setImage:[UIImage imageNamed:@"arrowdown-icon"] forState:UIControlStateNormal];
        [showBtn setImage:[UIImage imageNamed:@"arrowup-icon"] forState:UIControlStateSelected];
        [showBtn addTarget:self action:@selector(showContent:) forControlEvents:UIControlEventTouchUpInside];
        self.showBtn = showBtn;
        [self addSubview:showBtn];
        
        //内容
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.font = [UIFont boldSystemFontOfSize:14];
        contentLabel.textColor = RGBColor(153, 153, 153);
        contentLabel.numberOfLines=0;
        self.contentLabel = contentLabel;
        [self addSubview:contentLabel];
        
        //cell间间隔
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = RGBColor(28, 32,53);
        self.lineView=lineView;
        [self addSubview:lineView];
    }
    return self;
}
#pragma mark -----展示图片
- (void)showContent:(UIButton *)button
{
    button.selected = !button.selected;
    self.helpFrame.showAllFlag = button.selected;
    if (button.isSelected) {
        self.contentLabel.hidden=NO;
    }else{
        self.contentLabel.hidden=YES;
    }
    if ([self.delegate respondsToSelector:@selector(showAllContentsWithCell:)]){
        [self.delegate showAllContentsWithCell:self];
    }
}
- (void)setHelpFrame:(HelpFrame *)helpFrame {
    _helpFrame = helpFrame;
    [self setupLayout];
    [self setupData];
}
- (void)setupLayout{
    _answerLabel.frame = _helpFrame.answerLabelFrame;
    _showBtn.frame = _helpFrame.showBtnFrame;
    _contentLabel.frame = _helpFrame.contentLabelFrame;
    _lineView.frame = _helpFrame.lineViewFrame;
    
}

- (void)setupData{
    HelpModel *helpModel = _helpFrame.helpModel;
    self.answerLabel.text=helpModel.title;
    
    NSString * htmlString = [NSString stringWithFormat:@"%@",helpModel.content];
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    [attrStr addAttribute:NSForegroundColorAttributeName value:RGBColor(153, 153, 153) range:NSMakeRange(0, attrStr.length)];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attrStr.length)];
    self.contentLabel.attributedText=attrStr;
//    self.contentLabel.text=helpModel.content;
    
    self.contentLabel.hidden =!_helpFrame.showAllFlag;
    [self.showBtn setSelected:_helpFrame.showAllFlag];
    
    
    if ([helpModel.content isEqualToString:@""]) {
        self.showBtn.hidden=YES;
    }else{
        self.showBtn.hidden=NO;
    }
    
    
}











@end
